package com.jk.util;

import com.jk.vo.CouponInfo;
import com.jk.vo.CouponUser;
import org.apache.ibatis.jdbc.SQL;

public class TutorDynaSqlProvider {

    public String queryCoupon(final CouponInfo coupon) {

        return new SQL() {{
            SELECT("t1.coupon_id as couponId, t1.coupon_over_time as couponOverTime," +
                            " t1.coupon_type as couponType,t1.coupon_count as couponCount," +
                            " t1.coupon_quota as couponQuota, t1.coupon_min_aoc as couponMinAoc," +
                            " t1.coupon_start_time as couponStartTime ,t1.user_id as userId," +
                            " t2.user_name as userName");
            FROM("coupon_info t1");
            LEFT_OUTER_JOIN("user_login t2 ON t1.user_id = t2.user_id ");
            if (coupon != null){
                if (coupon.getUserId() != null) {

                WHERE("t1.user_id='"+coupon.getUserId()+"'");
            }
        }
        }}.toString();
    }
    public String queryUserCoupon(final CouponInfo coupon) {

        return new SQL() {{
            SELECT("t1.`coupon_id` AS couponId ,t1.`coupon_create_time` AS couponCreateTime ,\n" +
                    "t1.`coupon_Status` AS couponStatus ,t1.`user_id` AS userId ,\n" +
                    "t2.coupon_over_time AS couponOverTime, \n" +
                    "t2.coupon_type AS couponType,t2.coupon_count AS couponCount, \n" +
                    "t2.coupon_quota AS couponQuota, t2.coupon_min_aoc AS couponMinAoc, \n" +
                    "t2.coupon_create_time AS couponCreateTime ,t2.coupon_status AS couponStatus, \n" +
                    "t2.coupon_start_time AS conuponStartTime ,t2.user_id AS userId," +
                    "t3.user_name as userName ");
            FROM("coupon_user t1");
            LEFT_OUTER_JOIN("coupon_info t2 ON t1.coupon_id = t2.coupon_id ");
            LEFT_OUTER_JOIN("user_login t3 ON t2.user_id = t3.user_id ");
            if (coupon != null){
                if (coupon.getUserId() != null) {

                WHERE("t2.user_id='"+coupon.getUserId()+"'");
            }
        }
        }}.toString();
    }
    public String insertSelective(final CouponUser couponUser) {

        return new SQL() {{
            INSERT_INTO("coupon_user");
            if (couponUser.getCouponId() != null) {
                VALUES("coupon_id", "#{couponId}");
            }
            if (couponUser.getUserId() != null) {
                VALUES("user_id", "#{userId}");
            }
            if (couponUser.getCouponStatus() != null) {
                VALUES("coupon_Status", "#{couponStatus}");
            }
            if (couponUser.getCouponCreateTime() != null) {
                VALUES("coupon_create_time", "#{couponCreateTime}");
            }
        }}.toString();
    }
    public String updateByPrimaryKey(final String couponId) {

        return new SQL() {{
            UPDATE("coupon_info");

            SET("coupon_count = coupon_count - 1");

            WHERE("coupon_id = #{couponId}");
        }}.toString();
    }


    public String updateByPrimaryKeySelective(final CouponUser couponUser) {

        return new SQL() {{
            UPDATE("coupon_user");

            SET("coupon_Status = 3");

            WHERE("coupon_Status = 1 and ");
        }}.toString();
    }
}
